import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root'
})
export class MoldService {
  constructor(private http:HttpClient) { }
  private Mould = '/Mould';
  private createPaginationParams(currentPage: number, itemsPerPage: number): HttpParams {
    return new HttpParams()
      .set('page', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  }// 创建分页参数

  CreateMouldType(data:any){
    return this.http.post(environment.apiUrl+ this.Mould+'/CreateMouldType',data)
  }// 创建模具类型

  DeleteMouldType(id:any){
    return this.http.delete(environment.apiUrl+ this.Mould+'/DeleteMouldType',{params:{id:id}})
  }// 删除模具类型

  UpdateMouldType(data:any,id:any){
    return this.http.put(environment.apiUrl+ this.Mould+'/UpdateMouldType',data,{params:{id:id}})
  }// 更新模具类型

  GetMouldTypesWithChildren(page?: number, pageSize?: number){
    let params = new HttpParams();
    if (page !== undefined && pageSize !== undefined) {
      params = this.createPaginationParams(page, pageSize);
    }
    return this.http.get(environment.apiUrl+ this.Mould+'/GetPagedMouldTypesWithChildren',{
      params: params
    })
  }// 获取所有模具类型
  CreateMould(data:any){
    return this.http.post(environment.apiUrl+ this.Mould+'/CreateMould',data)
  }// 创建模具
  UpdateMould(data:any,id:any){
    return this.http.put(environment.apiUrl+ this.Mould+'/UpdateMould',data,{params:{id:id}})
  }// 更新模具
  DeleteMoulds(ids:any){
    const url = this.Mould+'/DeleteMoulds';
    const options = {
      body: ids
    };
    return this.http.delete(environment.apiUrl+ url,options)
  }// 删除模具

  GetMoulds(MouldTypeId:any,Status:any,SearchTerm?:any,Model?:any,page?: number, pageSize?: number){
    let params = new HttpParams();
    if (MouldTypeId ) {
      params = params.set('MouldTypeId', MouldTypeId);
    }
    if (Status ) {
      params = params.set('Status', Status);
    }
    if (SearchTerm ) {
      params = params.set('SearchTerm', SearchTerm);
    }
    if (Model ) {
      params = params.set('Model', Model);
    }
    if (page !== undefined && pageSize !== undefined) {
      params = params.set('PageNumber', page.toString());
      params = params.set('PageSize', pageSize.toString());
    }
    return this.http.get(environment.apiUrl+ this.Mould+'/GetMoulds',{
      params: params
    })
  }// 获取所有模具
 
  GetMouldById(id:any){
    return this.http.get(environment.apiUrl+ this.Mould+'/GetMouldById',{
      params: {id:id}
    })
  }// 获取模具
}
